package cn.itrip.search.service;

import cn.itrip.beans.vo.hotel.SearchHotCityVO;
import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.search.beans.Hotel;
import cn.itrip.search.dao.SolrBaseDao;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service("searchService")
public class SearchServiceImpl implements  SearchService{
//    HttpSolrClient   //通过这个对象向solr发送http请求，拿到相关数据
//         SolrQuery //通过这个类的对象设置具体需要的查询参数，然后把这个对象放入HttpSolrClient
//    QueryResponse//这个对象， 将solr返回的数据接收过来
    @Override
    public List<Hotel> getAllHotelList(SearchHotCityVO searchHotelVO) throws Exception {
        SolrBaseDao basedao = new SolrBaseDao("http://localhost:8090/solr/hotel");
        SolrQuery solrQuery = new SolrQuery();
        List<Hotel> hotelList = new ArrayList<Hotel>();
        solrQuery.setQuery("cityId:"+searchHotelVO.getCityId());
        solrQuery.setRows(searchHotelVO.getCount());
        solrQuery.setSort("id",SolrQuery.ORDER.desc);

        hotelList = basedao.getAllList(solrQuery,Hotel.class);
        System.out.println("——查询全部集合——hoelList大小为————"+hotelList.size());


        return hotelList;
    }

    @Override
    public Page getLimitHotelList(SearchHotelVO searchHotelVO, int currNo, int pageSize) throws Exception {
        SolrQuery  query=new SolrQuery();
        int tempFlag=0;
        StringBuffer  buffer=new StringBuffer();
        if(!EmptyUtils.isEmpty(searchHotelVO.getDestination())){
            buffer.append("destination:"+searchHotelVO.getDestination());
            tempFlag=1;
        }
        //keywords (string): [非必填] 关键词
        if (EmptyUtils.isNotEmpty(searchHotelVO.getKeywords())) {
            if (tempFlag == 1) {
                buffer.append(" AND keyword :" + searchHotelVO.getKeywords());
            } else {
                buffer.append(" keyword :" + searchHotelVO.getKeywords());
            }
        }
        if(!EmptyUtils.isEmpty(searchHotelVO.getHotelLevel())){
            query.addFilterQuery("hotelLevel :"+searchHotelVO.getHotelLevel());
        }

        if(!EmptyUtils.isEmpty(searchHotelVO.getFeatureIds())){
            int flat=0;
            StringBuffer  _buffer=new StringBuffer();
            String [] ids=searchHotelVO.getFeatureIds().split(",");
            for(String id:ids) {
                if (flat == 0) {
                    _buffer.append("featureIds:" +id);
                } else {
                    _buffer.append("OR featureIds:"+id);
                }
                flat++;
            }
            query.addFilterQuery("featureIds:"+buffer.toString());
        }


        if(EmptyUtils.isNotEmpty(searchHotelVO.getMaxPrice())){
            String ss="maxPrice :"+"["+searchHotelVO.getMaxPrice()+" TO *]";
            query.addFilterQuery(ss);
        }
        if(EmptyUtils.isNotEmpty(searchHotelVO.getMinPrice())){
            String ss="minPrice :"+"[* TO"+searchHotelVO.getMinPrice()+"]";
            query.addFilterQuery(ss);
        }

        if(EmptyUtils.isNotEmpty(searchHotelVO.getTradeAreaIds())){
            StringBuffer _buffer = new StringBuffer("(");
            int flag = 0;
            String tradeAreaIdArray[] = searchHotelVO.getTradeAreaIds().split(",");
            for (String tradeAreaId : tradeAreaIdArray) {
                if (flag == 0) {
                    _buffer.append(" tradingAreaIds:" + "*," + tradeAreaId + ",*");
                } else {
                    _buffer.append(" OR tradingAreaIds:" + "*," + tradeAreaId + ",*");
                }
                flag++;
            }
            _buffer.append(")");
            query.addFilterQuery(_buffer.toString());
        }
        if(!EmptyUtils.isEmpty(searchHotelVO.getAscSort())){
            query.setSort(searchHotelVO.getAscSort(), SolrQuery.ORDER.asc);
        }
        if(!EmptyUtils.isEmpty(searchHotelVO.getDescSort())){
            query.setSort(searchHotelVO.getDescSort(),SolrQuery.ORDER.desc);
        }

        if(EmptyUtils.isNotEmpty(buffer)){
            query.setQuery(buffer.toString());
        }
        SolrBaseDao base=new SolrBaseDao("http://localhost:8090/solr/hotel");
        Page page=base.getLimitPage(query,currNo,pageSize,Hotel.class);
        return page;
    }
}
